package cn.mdmm.controller.singlepage;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.mdmm.common.MessageConstant;
import cn.mdmm.common.SpringSecurityUtils;
import cn.mdmm.controller.BaseController;
import cn.mdmm.entity.response.ResponseBodyToGridData;
import cn.mdmm.entity.response.ResponseBodyToOne;
import cn.mdmm.entity.singlepage.SinglePageInfoModel;
import cn.mdmm.entity.system.SysUser;
import cn.mdmm.service.singlepage.SinglePageInfoService;
import cn.mdmm.service.system.SysDictService;
import cn.mdmm.service.type.TypeInfoService;
import cn.modoumama.common.annotation.OpLog;
import cn.modoumama.common.exception.ServiceException;
import cn.modoumama.common.exception.SystemException;
import cn.modoumama.common.utils.JSONUtils;
import cn.modoumama.page.GridDataModel;
import cn.modoumama.page.Page;

@Controller
@RequestMapping(value = "/singlepage")
@OpLog(logDesc="单页管理")
public class SinglePageController extends BaseController {
	
	@Autowired
	SinglePageInfoService singlePageInfoService;
	@Autowired
	TypeInfoService typeInfoService;
    @Autowired
    private SysDictService sysDictService;
	

	private Map<String, String> typeMap;
	
	public void init() {
		if(typeMap == null || typeMap.size()==0){
			typeMap = typeInfoService.getAllChildrenTypeToMap(2);
		}
	}
	
	/**
	 * 单页管理默认页面
	 * @param request
	 * @param newsType
	 * @return
	 */
	@RequestMapping(value = "init")
	public String init(HttpServletRequest request, Integer newsType){
		init();
        Map<String, String> statusMap = sysDictService.getDetailValueMap("状态");
        request.setAttribute("statusMap", JSONUtils.toJson(statusMap));
        request.setAttribute("statusCombo", JSONUtils.toCombo(statusMap));
		request.setAttribute("newsTypeMap", JSONUtils.toJson(typeMap));
		request.setAttribute("typeCombo", JSONUtils.toCombo(typeMap));
		return "singlepage/init";
	}
	
	/**
	 * 获取单页列表
	 * @param request
	 * @param response
	 * @param title
	 * @param newsType
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	@OpLog(logDesc="获取单页列表")
	public ResponseBodyToGridData findByConditions(HttpServletRequest request,String title, Integer type){
		ResponseBodyToGridData responseBodyToGridData = new ResponseBodyToGridData();
		try{
			Page po = getPage(request, null);
			po.getCondition().put("title", title);
			po.addCondition("type", type);
			GridDataModel gridDataModel = singlePageInfoService.findGridDataModelByCondition(po);
			responseBodyToGridData.setData(gridDataModel);
		} catch(Exception e){
			logger.error("获取单页列表异常", e);
			responseBodyToGridData.addErrInfo("999", "获取单页列表异常", null);
		}
		return responseBodyToGridData;
	}
	
	@RequestMapping(value = "showAdd")
	public String showAdd(HttpServletRequest request){
		init();
		request.setAttribute("typeCombo", JSONUtils.toCombo(typeMap));
		request.setAttribute("type", 2);
		return "singlepage/add";
	}
	
	/**
     * @Description: 新增单页
     * @param request
     * @param multipartFile
    */
	@RequestMapping(value = "insert")
	@OpLog(logDesc="新增单页")
	public String insert(HttpServletRequest request, SinglePageInfoModel singlePageInfo){
		Date curr = new Date();
		//当前登录的用户
		SysUser sysUser = SpringSecurityUtils.getCurrentUser();
		singlePageInfo.setCreateBy(String.valueOf(sysUser.getUserName()));
		singlePageInfo.setCreateDate(curr);
		singlePageInfo.setEditor(sysUser.getUserName());
		singlePageInfo.setEditDate(curr);
		singlePageInfo.setBrowseNum(0);
		try {
			singlePageInfoService.insert(singlePageInfo);
		} catch (SystemException e) {
			return "error";
		}
		return "success";
	}
	
	@RequestMapping(value = "showEdit")
	public String showEdit(HttpServletRequest request,Integer id){
		init();
		request.setAttribute("typeCombo", JSONUtils.toCombo(typeMap));
		SinglePageInfoModel singlepageInfo = singlePageInfoService.findById(id);
		request.setAttribute("singlepageInfo", singlepageInfo);
		return "singlepage/edit";
	}
	
	/**
     * @Description: 修改单页
     * @param request
     * @param multipartFile
     * @return
    */
	@RequestMapping(value = "update")
	@OpLog(logDesc="修改单页")
	public String update(HttpServletRequest request, SinglePageInfoModel singlePageInfo){
		Date curr = new Date();
		//当前登录的用户
		SysUser sysUser = SpringSecurityUtils.getCurrentUser();
		singlePageInfo.setEditor(sysUser.getUserName());
		singlePageInfo.setEditDate(curr);
		try {
			singlePageInfoService.mergeById(singlePageInfo);
		} catch (SystemException e) {
			return "error";
		}
		return "success";
	}

	@RequestMapping(value = "delete")
	@ResponseBody
	@OpLog(logDesc="删除单页")
	public ResponseBodyToOne delete(HttpServletRequest request,Integer[] ids){
		ResponseBodyToOne responseBody = new ResponseBodyToOne();
		logger.debug("method: delete() ");
		try {
			singlePageInfoService.removeByIds(ids);
		} catch (Exception e) {
			throw new ServiceException(MessageConstant.SYSTEM_ERROR);
		}
		return responseBody;
	}
	
	@RequestMapping(value = "info")
	@OpLog(logDesc="查看单页")
	public String info(HttpServletRequest request,Integer id){
		init();
		SinglePageInfoModel singlePageInfo = singlePageInfoService.findById(id);
		request.setAttribute("singlePageInfo", singlePageInfo);
		return "singlepage/info";
	}
}
